#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2021-11-29 11:24
# @Author : huabao.tan
# @File : demo1.py
# @Software: PyCharm
import random


class A:
    def f1(self):
        print('in A')


class Foo(A):
    def f1(self):
        super(Foo, self).f1()
        print('in Foo')


# a = Foo()
# print(a.f1())

class Bar(A):
    def f1(self):
        print('in Bar')


class InFo(Foo, Bar):
    def f1(self):
        super(InFo, self).f1()
        print('in InFo f1')


# b = InFo()
# print(InFo.mro())
# b.f1()
# super()并不是执行父类的方法
# 单继承super()肯定是继承父类的方法
# 多继承：严格按照self从属类的mro的执行顺序，执行s类的下一位


class QQpay:
    def pay(self, money):
        print(f'利用了qq支付了{money}')


class Alipay:
    def pay(self, money):
        print(f'利用了支付宝支付了{money}')


obj = QQpay()
obj1 = Alipay()

import time
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select
from Utils.logger import Log
from Utils.getPath import join_path

# 获取日志
getLogger = Log("basepage")
logger = getLogger.get_log()


class BasePage:
    # 初始化driver 窗口最大化  隐式等待
    def __init__(self, driver):
        self.driver = driver
        self.driver.implicitly_wait(0.2)
        self.driver.maximize_window()

    # 访问url
    def visit(self, url):
        self.driver.get(url)

    # 查找单个元素
    def find_element(self, locator):
        try:
            self.ele = self.driver.find_element(*locator)
            logger.info("成功定位元素 %s,%s " % (locator[0], locator[1]))
            return self.ele
        except Exception as e:
            logger.error("元素 %s,%s 查找失败, 原因: %s" % (locator[0], locator[1], str(e)))
            return False

    # 查找多个元素
    def findElements(self, locator):
        try:
            self.ele = self.driver.find_elements(*locator)
            logger.info("成功定位元素 %s,%s " % (locator[0], locator[1]))
            return self.ele
        except Exception as e:
            logger.error("元素 (%s,%s) 查找失败, 原因: %s" % (locator[0], locator[1], str(e)))
            return False

    # 点击
    def click(self, locator):
        try:
            self.find_element(locator).click()
            logger.info("点击元素(%s,%s) 成功" % (locator[0], locator[1]))
        except Exception as e:
            logger.error("点击元素(%s,%s) 失败， 原因:%s" % (locator[0], locator[1], str(e)))

    def get_screenshot_as_file(self):
        # 路径相对run.py而言
        rq = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
        self.screen_name = "ScreenImages\\" + rq + '.png'
        self.screen_image_path = join_path(self.screen_name)
        print(self.screen_image_path)
        try:
            self.driver.get_screenshot_as_file(self.screen_image_path)
            logger.info("截图成功了， 文件保存至---》 %s" % self.screen_image_path)
        except Exception as e:
            logger.error("截图失败了！！！！！！！！, 原因：%s" % e)

    # 输入文本
    def input(self, locator, value):
        self.find_element(locator).send_keys(value)

    # 睡眠
    def sleep(self, time):
        sleep(time)

    # 判断元素是否存在
    def isElementExist(self, locator):
        try:
            self.driver.find_element(*locator)
            return True
        except:
            return False

    # 获取元素的文本内容
    def get_ele_text(self, locator):
        texts = []
        isEle = self.isElementExist(locator)  # 判断元素是否存在页面
        if isEle:
            self.ele = self.findElements(locator)
            for i in self.ele:
                texts.append(i.text)
            logger.info("成功从元素 %s 中提取文本内容, 文本为 %s" % (locator, texts))
        return texts

    # 获取当前页面url
    def get_current_url(self):
        return self.driver.current_url

    # 浏览器前进操作
    def forward(self):
        try:
            self.driver.forward()
            logger.info("页面前进成功!!!")
        except Exception as e:
            logger.error("页面前进失败 ,原因: " + str(e))

    # 页面刷新
    def refresh(self):
        try:
            self.driver.refresh()  # 刷新当前页面
            logger.info("页面刷新成功!!")
        except Exception as e:
            logger.error("页面刷新失败:  " + str(e))

    # 浏览器后退操作
    def back(self):
        try:
            self.driver.back()
            logger.info("页面后退成功!!!")
        except Exception as e:
            logger.error("页面后退失败 ,原因: " + str(e))

        # 隐式等待
        def wait(self, seconds):
            self.driver.implicitly_wait(seconds)
            logger.info("设置隐式时间：%d 秒." % seconds)

        # 点击关闭当前窗口
        def close(self):
            try:
                self.driver.close()
                logger.info("成功关闭当前窗口")
            except NameError as e:
                logger.error("当前窗口关闭失败 原因: %s" % e)

    # 切到iframe
    def switch_frame(self, locator):
        iframe = self.find_element(locator)
        try:
            self.driver.switch_to.frame(iframe)  # 原来的driver.switch_to_frame(iframe)已被弃用了
            logger.info("iframe切换成功 该元素为 %s,%s " % (locator[0], locator[1]))
        except NameError as e:
            logger.error("iframe切换失败  该元素为 %s,%s   %s" % (locator[0], locator[1], str(e)))

    # 切换到上一级iframe
    def quit_Switch_frame(self):
        try:
            self.driver.switch_to.parent_frame()
            logger.info("成功切换到上一级iframe成功")
        except Exception as e:
            logger.error("切换到上一级iframe失败" + str(e))

    # 切花到iframe主文档  即(页面上的第一个框架，或者当页面包含 iframe 时选择主文档)
    def switch_defalut(self):
        try:
            self.driver.switch_to.default_content()
            logger.info("成功切换到主iframe")
        except Exception as e:
            logger.error("切换到主iframe失败  原因: " + str(e))

    # 选择 select 下拉框
    def select(self, locator, select_type, content):
        try:
            ele_select = self.find_element(locator)
            if select_type == 'index':
                Select(ele_select).select_by_index(content)
            elif select_type == 'value':
                Select(ele_select).select_by_value(content)
            elif select_type == 'text':
                Select(ele_select).select_by_visible_text(content)
            else:
                logger.error("不支持{},请确认Select的类型".format(select_type))
                return
            logger.info("select选中成功  根据元素<{}>以 {} 方式进行下拉选择".format(locator, select_type))
        except Exception as e:
            logger.error("select选中失败了  根据元素<%s>以%s方式进行下拉选择失败！原因: %s" % (locator, select_type, str(e)))

    # 取消选中 select 下拉框
    def deselect(self, locator, *tc):
        ele_select = self.find_element(locator)
        try:
            if tc[0] == "all":
                Select(ele_select).deselect_all()
            elif tc[0] == "index":
                Select(ele_select).deselect_by_index(tc[1])
            elif tc[0] == "value":
                Select(ele_select).deselect_by_value(tc[1])
            elif tc[0] == "text":
                Select(ele_select).deselect_by_visible_text(tc[1])
            else:
                logger.error("不支持{},请确认Select的类型".format(tc[0]))
                return
            logger.info("取消选中select框成功  根据元素<{}>以 {} 方式进行取消选择".format(locator, tc[0]))
        except Exception as e:
            logger.error("取消选择select失败了  根据元素<%s>以%s方式进行下拉选择失败！原因: %s" % (locator, tc[0], str(e)))

    # 获取select框选中值
    def get_select(self, locator, get_select_type):
        '''
        获取select框选中值
        options                  # 返回select元素所有的options
        all_selected_options     # 返回select元素中所有已选中的选项
        first_selected_options   # 返回select元素中选中的第一个选项
        :return:
        '''
        ele_select = self.find_element(locator)
        try:
            if get_select_type == "all":
                options = Select(ele_select).options
            elif get_select_type == "select":
                options = Select(ele_select).all_selected_options
            elif get_select_type == "select_first":
                options = Select(ele_select).first_selected_option
            else:
                logger.error("类型错误,你输入的类型为: %s  类型应该为 all || select || select_first" % get_select_type)
                return
            logger.info("获取options成功, 你所用的获取类型为 %s" % get_select_type)
            return options
        except Exception as e:
            logger.error("获取options失败了, 你所用的类型为: %s 原因: %s" % (get_select_type, str(e)))

    u"""鼠标悬停操作"""

    def move_is_element(self, locator):
        try:
            element = self.find_element(locator)
            ActionChains(self.driver).move_to_element(element).perform()
            logger.info("悬停成功 该元素为 %s,%s" % (locator[0], locator[1]))
        except Exception as e:
            logger.error("鼠标悬停失败 该元素为 %s,%s 原因: %s" % (locator[0], locator[1], str(e)))

    u"""获取当前窗口的title"""

    def get_title(self):
        try:
            title = self.driver.title
            logger.info("成功获取当前窗口 title为:  {} ".format(title))
            return title
        except Exception as e:
            logger.error("获取当前窗口 title 失败: 原因: %s" + str(e))

    u"""获取属性"""

    def get_attribute(self, locator, name):
        return self.find_element(locator).get_attribute(name)

    # 执行JS脚本
    def execute_script(self, script):
        try:
            self.driver.execute_script(script)
            logger.info("js脚本执行成功")
        except Exception as e:
            logger.error("js脚本执行失败  原因: " + str(e))

    u"""滑动到页面顶部"""

    def js_scroll_top(self):
        js = "window.scrollTo(0,0)"
        self.driver.execute_script(js)

    u"""滑动到页面底部"""

    def js_scroll_end(self):
        js = "window.scrollTo(0, document.body.scrollHeight)"
        self.driver.execute_script(js)

    # 获取 弹窗
    def alert(self):
        try:
            alert = self.driver.switch_to.alert
            logger.info("成功获取弹窗")
            return alert
        except Exception as e:
            logger.error("获取弹窗失败了 原因: " + str(e))

    # 弹窗 确定
    def alert_accept(self):
        try:
            self.alert().accept()
            logger.info("alert弹窗确认成功")
        except Exception as e:
            logger.error("alert弹窗确认 失败!!原因: " + str(e))

    # 获取弹窗 文本值
    def alert_text(self):
        try:
            text = self.alert().text
            logger.info("获取alert弹窗值 %s 成功 " % text)
            return text
        except Exception as e:
            logger.error("获取alert弹窗值 失败 原因: " + str(e))

    # 取消弹窗
    def alert_dismiss(self):
        try:
            self.alert().dismiss()
            logger.info("已取消alert弹窗")
        except Exception as e:
            logger.error("取消alert弹窗失败!! 原因: " + str(e))

    # 弹窗 输入
    def alert_prompt(self, value):
        try:
            self.alert().send_keys(value)  # 这里输入框中看不到输入的文字  alter和confirm没有输入对话框，所以这里不能用，只能用于prompt
            logger.info("成功在弹窗alert中 输入 %s" % value)
        except Exception as e:
            logger.error("在alert弹窗输入值失败 原因: " + str(e))

    # 退出浏览器
    def quit(self):
        self.driver.quit()




def getUserName(txt):
    first_name=random.choice(txt)
    second_name = random.choice(txt)
    last_name = random.choice(txt)
    return first_name+second_name+last_name


list = ['还','谭','华','宝']
print(getUserName(list))
